﻿using System;
using System.Linq;
using System.Web.Mvc;
using MiniWeb.Core.Entities;
using MiniWeb.Task;
using MiniWeb.Web.Controllers.Admin.ProductsImgInfoManager.ViewModels;
using WFramework.Extensions;
using WFramework.Mappers;
using WFramework.MvcPager;
using WFramework;
using System.Collections.Generic;
namespace MiniWeb.Web.Controllers.Admin.ProductsImgInfoManager
{

    public class ProductsImgInfoManagerController : BaseController
    {
        private readonly ProductsImgInfoTask _productsimginfoTask = new ProductsImgInfoTask();
        private readonly ProductsInfoTask _productsInfoTask = new ProductsInfoTask();

        public ActionResult List(int pid,int pageIndex = 1, int pageSize = 20)
        {
            var productinfo = _productsInfoTask.GetById(pid);
            if(productinfo==null)
            {
                return new EmptyResult();
            }
            ViewData["promodel"] = productinfo;
            var pagedList = _productsimginfoTask.GetPagedList(pid,pageIndex, pageSize);
            //ViewData["plist"]=_productsInfoTask.GetAll(100,"", null).ToSelectListForAll(c=>c.ID.ToString(),c=>c.ProductName);
            var model = new PagedList<ProductsImgInfo>(pagedList.ToList(), pageIndex, pageSize, pagedList.TotalItemCount);

            return View(model);
        }


        public ActionResult Save(int? id,int pid)
        {
            var model = new SaveProductsImgInfo();
            var productinfo = _productsInfoTask.GetById(pid);

            if (productinfo == null)
            {
                return new EmptyResult();
            }
            model.ProductsID = pid;

            ViewData["promodel"] = productinfo;
            if (id != null)
            {
                var item = _productsimginfoTask.GetById((int)id);
                if (item == null)
                    return AlertMsg("参数错误", HttpContext.Request.UrlReferrer.PathAndQuery);

                model = EntityMapper.Map<ProductsImgInfo, SaveProductsImgInfo>(item);
            }
            return View(model);
        }

        [HttpPost]
        public ActionResult Save(SaveProductsImgInfo savemodel)
        {

            if (ModelState.IsValid)
            {
                string errMsg, savePath = string.Empty;
                var dict = new Dictionary<string, string>();
                if (savemodel.ID == null)
                {

                    foreach (var key in Request.Files.AllKeys)
                    {
                        if (Request.Files[key] == null || Request.Files[key].ContentLength == 0)
                        {
                            ModelState.AddModelError(key, "请上传");
                            return View(savemodel);
                        }

                        if (!UploadService.UploadFile(Request.Files[key], out savePath, out errMsg))
                        {
                            return AlertMsg(errMsg, HttpContext.Request.UrlReferrer.PathAndQuery);
                        }
                        dict.Add(key, savePath);
                    }

                    var model = new ProductsImgInfo
                                    {
                                        ProductsID = savemodel.ProductsID,
                                        ProductName = _productsInfoTask.GetById(savemodel.ProductsID).ProductName,
                                        ProductsImg = dict["ProductsImg"],
                                        CreateDate = DateTime.Now,
                                    };
                    _productsimginfoTask.Add(model);
                }
                else
                {
                    var model = _productsimginfoTask.GetById((int)savemodel.ID);

                    foreach (var key in Request.Files.AllKeys.Where(key => Request.Files[key] != null && Request.Files[key].ContentLength > 0))
                    {

                        if (!UploadService.UploadFile(Request.Files[key], out savePath, out errMsg))
                        {
                            return AlertMsg(errMsg, HttpContext.Request.UrlReferrer.PathAndQuery);
                        }
                        dict.Add(key, savePath);
                    }

                    if (model == null)
                        return AlertMsg("参数错误", HttpContext.Request.UrlReferrer.PathAndQuery);

                    model.ProductsID = savemodel.ProductsID;
                    model.ProductName = _productsInfoTask.GetById(savemodel.ProductsID).ProductName;
                    model.ProductsImg = dict.ContainsKey("ProductsImg") ? dict["ProductsImg"] : model.ProductsImg;
                   


                    _productsimginfoTask.Update(model);
                }
                return AlertMsg("保存成功", HttpContext.Request.UrlReferrer.PathAndQuery);

            }

            ////////////////////////////////////////


            return View(savemodel);
        }

      

        #region 删除用户信息 Delete

        [HttpPost]
        public void Delete(int id)
        {
            _productsimginfoTask.Delete(id);
        }

        #endregion
    }
}